<template>
	<view class="container">
		<template v-if="Object.keys(projectData).length > 0">
			<!-- 确认项目信息 -->
			<view class="u-padding-20 u-margin-bottom-20 z-b-r white-bg">
				<view class="u-margin-bottom-10">
					<u-tag text="当前打卡项目信息" type="success" />
				</view>
				<view class="u-margin-bottom-10">
					<text>项目名称：{{ projectData.project_name || '加载中……' }}</text>
				</view>
				<view class="u-margin-bottom-10">
					<text>项目经理姓名：{{ projectData.pm_name || '加载中……' }}</text>
				</view>
				<view class="u-margin-bottom-10">
					<text>项目经理电话：{{ projectData.pm_phone || '加载中……' }}</text>
				</view>
				<view class="u-margin-bottom-10">
					<text>项目所在地：{{ projectData.project_address || '加载中……' }}</text>
				</view>
				<view class="u-margin-bottom-10">
					<text>项目开始时间：{{ projectData.project_begin_date || '加载中……' }}</text>
				</view>
			</view>
			<view class="u-padding-20 white-bg z-b-r">
				<u-form :model="form" :border-bottom="false" ref="uForm" label-width="auto">
					<u-form-item label-width="80" required="true" label="孔深" prop="hole_deep">
						<u-input v-model="form.hole_deep" placeholder="请输入孔深(单位/米)" />
					</u-form-item>
					<u-form-item label-width="80" required="true" label="孔号" prop="hole_mark">
						<u-input v-model="form.hole_mark" placeholder="请选择打卡孔号" type="select" :select-open="holeShow" @click="holeShow = true"/>
						<u-select v-model="holeShow" mode="single-column" @confirm="holeConfirm" :list="holeNums"></u-select>
					</u-form-item>
				</u-form>
				<u-button class='u-margin-20' :disabled="submitBtnDis" type="primary" @click="submit">点击打卡</u-button>
			</view>
		</template>
		<template v-else>
			<empty-data tipText="您没有正在进行的项目"></empty-data>
		</template>

		<u-toast ref="uToast" />
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {mapState} from "vuex";
	import {myTime} from "@/common/formatTime.js";
	export default {
		data() {
			return {
				holeShow:false,
				submitBtnDis:false,
				holeNums:[],
				projectData:{},
				form: {
					hole_deep: "", //孔深
					hole_mark: ""//孔号
					
				},
				rules: {
					hole_deep: [{
						required: true,
						message: "请输入钻孔深度",
						trigger: ['blur', 'change']
					}],
					hole_mark:[{
						required: true,
						message: "请输入孔号",
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		computed:{
			...mapState({
				token:"token",
				wkID:"wkID"
			})
		},
		onLoad() {
			//获取当前参与的项目
			this.getWorkerInProject()
		},
		onReady() {
			if(Object.keys(this.projectData).length > 0){
				this.$refs.uForm.setRules(this.rules);
			}
		},
		mounted() {
			
		},
		methods: {
			async getWorkerInProject(){
				let {token,wkID} = this;
				this.$loading("请稍后")
				let res = await this.$network({
					api:"main",
					data:{
						action:"wkSignAdmin/getUnderwayProject",
						data:{
							token,
							wkID,
							projectStatus:2
						}
					}
				})
				
				uni.hideLoading();
				if(res.status === 0){
					this.projectData = res.data[0];
					//删除已经结束的孔号
					this.projectData.holes.forEach((item,index)=>{
						if(item.status === 0){
							this.projectData.holes.splice(index,1);
						}
					})
					this.holeNums = this.projectData.holes.map(item=>{
						item.label = item.num;
						item.value = item.num;
						return item;
					})
					
				}
			},
			holeConfirm(e){
				this.form.hole_mark = e[0].value;
			},
			async submit(){
				let {token,projectData,wkID} = this;
				this.$refs.uForm.validate(async valid => {
					if (valid) {
						this.form.date = myTime(Date.now());
						this.form.projectID = projectData._id;
						this.form.pm_id = projectData.pm_id;
						this.form.signinStatus = 0; //0待审核，1审核通过，-1审核未通过
						this.form.wkID = wkID;
						this.submitBtnDis = true;
						this.$loading("正在打卡")
						let res = await this.$network({
							api: "main",
							data:{
								action:"wkSignAdmin/signin",
								data:{
									token,
									data:this.form
								}
							}
						});
						uni.hideLoading();
						this.submitBtnDis = false;
						if (res.status === 0) {
							this.form = {
								hole_deep: "",
								hole_mark: ""
							}
							this.$refs.uToast.show({
								title: '打卡成功，等待审核',
								type: 'success'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	
</style>
